package com.zixuan.gulicollege.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zixuan.gulicollege.common_utils.ResultCode;
import com.zixuan.gulicollege.eduservice.mapper.EduChapterMapper;
import com.zixuan.gulicollege.eduservice.service.EduChapterService;
import com.zixuan.gulicollege.eduservice.service.EduVideoService;
import com.zixuan.gulicollege.entity.eduservice.EduChapter;
import com.zixuan.gulicollege.entity.eduservice.EduVideo;
import com.zixuan.gulicollege.entity.eduservice.vo.ChapterVO;
import com.zixuan.gulicollege.entity.eduservice.vo.VideoVO;
import com.zixuan.gulicollege.exception_handler.ConstantExceptionInfo;
import com.zixuan.gulicollege.exception_handler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zixuan
 * @since 2021-05-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
        // 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        wrapperChapter.orderByAsc("sort");
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);

        // 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduVideo> videoList = videoService.list(wrapperVideo);

        List<ChapterVO> finaChapterVOList = new ArrayList<>();
        for (EduChapter eduChapter : chapterList) {
            // 遍历查询章节list集合进行封装
            // eduChapter复制
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(eduChapter, chapterVO);
            finaChapterVOList.add(chapterVO);

            List<VideoVO> videoVOList = new ArrayList<>();
            // 遍历查询小结list集合进行封装
            for (EduVideo eduVideo : videoList) {
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(eduVideo, videoVO);

                    videoVOList.add(videoVO);
                }
            }
            chapterVO.setChildren(videoVOList);
        }
        return finaChapterVOList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        // 根据章节id查询小结，如果有数据就不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);

        // 判断是否查询出数据
        if (count > 0) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.FAIL_DELETE_CHAPTER_HAVE_SUMMARY);
        } else {
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", id);
        baseMapper.delete(chapterWrapper);
    }
}
